package com.lzy.cloud.base.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzy.cloud.base.dto.BaseRuleDto;
import com.lzy.cloud.base.entity.BaseRule;
import com.lzy.cloud.base.mapper.BaseRuleMapperExt;
import com.lzy.cloud.base.service.RuleService;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RuleServiceImpl implements RuleService {

    @Autowired
    private BaseRuleMapperExt baseRuleMapperExt;

    @Override
    public Long insert(BaseRuleDto baseRuleDto) {
        BaseRule baseRule = DozerUtils.map(baseRuleDto, BaseRule.class);
        return baseRuleMapperExt.insertSelective(baseRule)>0?baseRule.getId():0;
    }

    @Override
    public boolean update(BaseRuleDto baseRuleDto) {
        return baseRuleMapperExt.updateByPrimaryKeySelective(DozerUtils.map(baseRuleDto, BaseRule.class)) > 0;
    }

    @Override
    public BaseRuleDto find(Long id) {
        return DozerUtils.map(baseRuleMapperExt.selectByPrimaryKey(id), BaseRuleDto.class);
    }

    @Override
    public PageInfo<BaseRuleDto> queryList(Pager pager, BaseRuleDto baseRuleDto) {
        if (pager != null)
            PageHelper.startPage(pager.getPageNum(), pager.getPageSize());
        return new PageInfo<>(baseRuleMapperExt.queryList(baseRuleDto));
    }

    @Override
    public boolean deleteById(Long id) {
        BaseRuleDto baseRuleDto = new BaseRuleDto();
        baseRuleDto.setId(id);
        baseRuleDto.setDeleted(true);
        return baseRuleMapperExt.updateByPrimaryKeySelective(DozerUtils.map(baseRuleDto, BaseRule.class)) > 0;
    }

    @Override
    public List<BaseRuleDto> queryList(BaseRuleDto baseRuleDto) {
        return baseRuleMapperExt.queryList(baseRuleDto);
    }


}
